<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <div class="base-content-operate">
        <el-button
          v-rights="'Create'"
          icon="el-icon-plus"
          type="primary"
          @click="onNew"
          >新增</el-button
        >
        <!-- v-rights="'Copy'" -->
        <el-button
          v-rights="'Copy'"
          v-loading="submitLoad"
          icon="el-icon-document-copy"
          type="primary"
          @click="copySamplingSingle"
          >复制</el-button
        >
        <!-- v-rights="'Export'" -->

        <!-- <el-button
          v-loading="submitLoad"
          icon="el-icon-document-copy"
          type="primary"
          @click="exportData"
          >导出</el-button
        > -->
        <!-- v-rights="'Delete'" -->

        <el-button
          v-rights="'Delete'"
          v-loading="submitLoad"
          icon="el-icon-delete"
          type="danger"
          @click="deleteSamplingSingle"
          >删除</el-button
        >
      </div>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="已提交" name="first"></el-tab-pane>
        <el-tab-pane label="草稿" name="second"></el-tab-pane>
      </el-tabs>
      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="采样单号">
          <template slot-scope="scope">
            {{ scope.row.samplingSingleNum }}
          </template>
        </el-table-column>
        <el-table-column label="被采样单位">
          <template slot-scope="scope">
            {{ scope.row.samplingUnitName }}
          </template>
        </el-table-column>
        <el-table-column label="采样地点">
          <template slot-scope="scope">
            {{ scope.row.samplingPositionName }}
          </template>
        </el-table-column>
        <el-table-column label="采样人">
          <template slot-scope="scope">
            {{ scope.row.samplingPeople }}
          </template>
        </el-table-column>
        <el-table-column label="采样时间">
          <template slot-scope="scope">
            {{ scope.row.samplingDateTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                v-if="activeName == 'first'"
                size="mini"
                type="primary"
                @click="select(scope.row)"
                >查看</el-link
              >
              <el-link
                v-if="activeName == 'second'"
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >编辑</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :current-page="pagination.pageIndex"
        :page-count="pagination.total"
        @changePage="changePage"
        @sizeChange="sizeChange"
      />
    </div>

    <SamplingSingleEdit
      v-if="dialogFormVisible"
      ref="SamplingSingleEdit"
      :dialog-form-visible.sync="dialogFormVisible"
      :read-only.sync="readOnly"
      :edit-id.sync="editId"
    />
  </div>
</template>

<script>
import {
  deleteRecord,
  getList,
  copy,
} from "../../../api/Sampling/SamplingSingle.js";
import SamplingSingleEdit from "./SamplingSingleEdit.vue";

export default {
  name: "SamplingSingleList",
  components: { SamplingSingleEdit },
  data() {
    return {
      activeName: "first",
      submitLoad: false,
      readOnly: false,
      editId: "",
      loading: true,
      list: [],
      tableDatas: [],
      queryForm: {
        samplingSingleCode: 0,
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 复制采样单
    copySamplingSingle() {
      var idList = [];
      for (let i = 0; i < this.tableDatas.length; i++) {
        idList.push(this.tableDatas[i].id);
      }
      if (idList === null || idList.length === 0) {
        this.$message.warning("请选择数据!");
        return false;
      }
      this.submitLoad = true;
      copy({ id: idList })
        .then((res) => {
          this.submitLoad = false;
          this.getList();
        })
        .catch(() => {
          this.submitLoad = false;
          this.getList();
        });
    },
    // 导出数据
    exportData() {},
    // 删除采样单
    deleteSamplingSingle() {
      var idList = [];
      for (let i = 0; i < this.tableDatas.length; i++) {
        idList.push(this.tableDatas[i].id);
      }
      if (idList === null || idList.length === 0) {
        this.$message.warning("请选择数据!");
        return false;
      }
      this.submitLoad = true;
      this.$confirm("是否确定删除采样记录?", "删除采样记录", {
        type: "warning",
        confirmButtonText: "确定",
        okType: "danger",
        center: true,
        cancelButtonText: "取消",
        beforeClose: (action, instance, done) => {
          if (action === "confirm") {
            deleteRecord({ id: idList })
              .then((res) => {
                this.submitLoad = false;
                done();
                this.getList();
              })
              .catch(() => {
                this.submitLoad = false;
              });
          } else {
            this.submitLoad = false;
            done();
          }
        },
      }).catch(() => {
        this.submitLoad = false;
      });
    },
    // 切换页签
    handleClick() {
      this.sizeChange();
    },
    handleSelectionChange(val) {
      this.tableDatas = val;
    },
    // 查看
    select(row) {
      this.readOnly = true;
      this.dialogFormVisible = true;
      this.editId = row.id;
    },
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.pagination.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      if (this.activeName === "first") {
        this.queryForm.samplingSingleCode = 20;
      } else if (this.activeName === "second") {
        this.queryForm.samplingSingleCode = 10;
      }

      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      // this.$refs.SamplingSingleEdit.formData = this.Helper.deepCopy(
      //   this.$refs.SamplingSingleEdit.initData
      // );
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(row) {
      this.dialogFormVisible = true;
      this.editId = row.id;
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.getList();
    },
  },
};
</script>
